<?php
class Integral_ruletypeModel extends Model {
    public $list;
	public function getList2($id=0,$i=0,$map = array()){
		if($id!=0)$map['r.id'] = $id;   
		$re = $this-> alias('r') -> where($map) -> Field('r.*') -> select();
		if(false == $re) return null;
		$this->list=$re;
		return $this->list;
	}
	public function getRuletype($isfix=''){
		if($isfix!=''){
			$map['isfix']=$isfix;
		}
		$map['site_id']=__SITEID__;
		return $this->where($map)->select();	
	}
	
	//输入一个ID，递归出一个上级ID为0的ID
	public function getTopId($pid){
		if($pid){
			$ret=$this->where(['id'=>$pid])->find();
			if($ret['pid']==0){
				return $pid;
			}else{
				return $this->getTopid($ret['pid']);
			}
		}else{
			return $pid;	
		}
	}
	
	//获取列表，没有
	function getTypeId($pid){
		$ret=$this->where('pid='.$pid.' and site_id='.__SITEID__)->getField('id',true);
		if($ret){
			foreach($ret as $v){
				$this->list[]=$v;
				array_merge($this->list,$this->getTypeId($v));
			}
		}else{
			return null;	
		}
		return $this->list;
	}
	//获取分类列表，并带上‘深度’
	public function getList($pid=0,$i=0,$id=0){
        $map['pid']=$pid;
		$map['site_id']=__SITEID__;
        if($id!=0){
            $map['id']=$id;    
        }
		$re = $this->where($map)->select();
		if(false == $re) return null;
		foreach ($re as $v){
			$v['cl']=$i;
		    $this->list[]=$v;
			array_merge($this->list,$this->getList($v['id'],$i+1));
		}
		return $this->list;
	}
	//复制数据
	public function copy($siteid, $tositeid,$map, $pid = 0){
		$re=$this->where($map)->order('id')->select();
		$newsDB = M('Integral_rule');
		$formdb = M('Form');
		if($re!==false){
			foreach($re as $k=>$v){
				$map['pid'] = $v['id'];
				$v['site_id'] = $tositeid;
				unset($v['id']);
				$v['pid'] = $pid;
				$pid1 = $this->data($v)->add();
				
				echo '<script>window.parent.progress("写入一个分类，ID为：'.$pid1.'");</script>';
				ob_flush();
                flush();
				
                //
                $newsdata = $newsDB -> where(array('type_id' => $map['pid']))-> select();
                foreach($newsdata as $val){
					//规则在表格里面,需要换掉表格里的ID
					$forms = $formdb -> where(array('ruleids' => array('like', '%,'.$val['id'].',%'), 'site_id' => $tositeid)) -> select();
					$oldruleids = $val['id'];
                    unset($val['id']);
                    $val['type_id'] = $pid1;
                    $val['site_id'] = $tositeid;
                    $news_id = $newsDB->data($val) -> add();
					
					//换掉表格的规则ID
					foreach($forms as $fkey => $fval){
						$fdata['ruleids'] = str_replace(','.$oldruleids.',', ','.$news_id.',', $fval['ruleids']);
						$formdb -> where(array('id' => $fval['id'])) -> data($fdata) -> save();
					}
					
                    echo '<script>window.parent.progress("写入一个产品，ID为：'.$news_id.'");</script>';
                    ob_flush();
                    flush();
                }
				$this->copy($siteid, $tositeid,$map, $pid1);
			}
			return true;
		}
	}
	//获取积分规则树
	public function getRuletypeTree($pid){
		if($pid){
			$pid=$pid;
		}elseif($pid===0){
			$pid=$pid;
		}else{
			$pid=$this->_param('pid');
		}
//		$pid =$pid ? $pid : $_POST['pid'];
        $map['pid'] = $pid;
        $map['site_id'] = __SITEID__;
        $ret = array();
        $list = $this -> where($map) -> select();
        foreach($list as $k => $v){
            //查找层级
			$sub = $this  -> where(array('pid' => $v['id'],'site_id'=>__SITEID__)) -> count();
            $isParent = false;
            if($sub > 0){
                $isParent = true;
				$data['son']=$this->getRuletypeTree($v['id']);
            }
            $data['isParent'] = $isParent;
            $data['id'] = $v['id'];
            $data['name'] = $v['name'];
            $ret[] = $data;
        }
        return ($ret);
    }
}
?>